package com.xiaoyun.avtiviti05;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.List;

/**
 * 组任务测试
 */
public class GroupTest {
    public static void main(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        TaskService taskService = processEngine.getTaskService();
        String key = "myProcess_1";
        String assignee = "zhangsan";
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee(assignee)
                .singleResult();

        if (task != null) {
            taskService.complete(task.getId());
            System.out.println("执行任务");
        }

    }


    public static void main7(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        TaskService taskService = processEngine.getTaskService();
        String key = "myProcess_1";
        String assignee = "zhangsan";
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee(assignee)
                .list();

        list.stream().forEach(
                task -> {
                    System.out.println(task.getProcessInstanceId());
                    System.out.println(task.getId());
                    System.out.println(task.getName());
                    System.out.println(task.getAssignee());
                }
        );

    }

    public static void main6(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        TaskService taskService = processEngine.getTaskService();

        String key = "myProcess_1";
        String candidate_users = "zhangsan";

        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskCandidateUser(candidate_users)
                .singleResult();

        if (task != null) {
            taskService.claim(task.getId(), candidate_users);
            System.out.println("任务拾取完毕");
        }
    }


    public static void main4(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        TaskService taskService = processEngine.getTaskService();
        String key = "myProcess_1";
        String candidate_users = "zhangsan";
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskCandidateUser(candidate_users)
                .list();
        list.stream().forEach(task -> {
            System.out.println(task.getProcessInstanceId());
            System.out.println(task.getId());
            System.out.println(task.getName());
            System.out.println(task.getAssignee());
        });
    }

    public static void main3(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        TaskService taskService = processEngine.getTaskService();
        String key = "myProcess_1";
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee("xiaozhang")
                .singleResult();

        if (task != null) {
            taskService.complete(task.getId());
            System.out.println(task.getId());
        }

    }

    public static void main2(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        RuntimeService runtimeService = processEngine.getRuntimeService();
        String key = "myProcess_1";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key);

        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getId());
    }


    public static void main1(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday4.bpmn")
                .name("请假申请单流程")
                .deploy();

        System.out.println(deployment.getId());
        System.out.println(deployment.getName());
    }
}
